package com.xfly.app.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.view.TitleBarView;

import java.util.HashMap;
import java.util.Map;

public class RePassActivity extends BaseMvpActivity {

    TitleBarView title_bar;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    EditText oldpass,newpass,newpass2;
    Button enter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_repass);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        enter = findViewById(R.id.enter);
        oldpass = findViewById(R.id.oldpass);
        newpass = findViewById(R.id.newpass);
        newpass2 = findViewById(R.id.newpass2);

        title_bar.setText("修改密码");
    }

    @Override
    public void initEvents() {

        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        newpass2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                enter.setBackgroundResource(R.drawable.btn_red2);
                enter.setEnabled(true);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reset();
            }
        });
    }

    private void  reset(){
        Map<String,String> map = new HashMap<>();
        map.put("oldpass",oldpass.getText().toString());
        map.put("password",newpass.getText().toString());
        map.put("repassword",newpass2.getText().toString());
        apiDataRepository.repass(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                showMsg("密码修改成功");
                finish();
            }
        });
    }
}
